<?php

/**
 *    文章管理控制器
 *
 *    @author    Hyber
 *    @usage    none
 */
class WeiboreplyApp extends BackendApp
{
	var $_weiboreply_mod;


	function __construct()
	{
		$this->WeiboreplyApp();
	}

	function WeiboreplyApp()
	{
		parent::BackendApp();
		$this->_weiboreply_mod =& m('weiboreply');
	}

	/**
	 *    文章索引
	 *
	 *    @author    Hyber
	 *    @return    void
	 */
	function index()
	{
        $search_options = array(
            'cw.title'   => Lang::get('title'),
            'cm.user_name'   => Lang::get('username'),
            'cm.real_name'   => Lang::get('realname'),
        );
        /* 默认搜索的字段是店铺名 */
        $field = 'course_name';
        array_key_exists($_GET['field'], $search_options) && $field = $_GET['field'];
        $conditions = $this->_get_query_conditions(array(array(
            'field' => $field,       //按用户名,店铺名,支付方式名称进行搜索
            'equal' => 'LIKE',
            'name'  => 'search_name',
        ),array(
            'field' => 'cr.replydate',
            'name'  => 'add_time_from',
            'equal' => '>=',
            'handler'=> 'gmstr2time',
        ),array(
            'field' => 'cr.replydate',
            'name'  => 'add_time_to',
            'equal' => '<=',
            'handler'   => 'gmstr2time_end',
        ),
        ));

		if(@$_GET['user']){
			$user=@$_GET['user'];
			$conditions.=" and cm.user_name like '%$user%' or  real_name like  '%$user%' "	;
		}
		$page   =   $this->_get_page(10);   //获取分页信息
		$sql="SELECT cr.*,cm.user_name,cm.real_name,cw.title as titlew FROM cw_weiboreply AS cr
                LEFT JOIN cw_member AS cm ON cr.uid=cm.user_id
                LEFT JOIN cw_weibo AS cw ON cw.wei_id = cr.wei_id
                WHERE 1=1 ".$conditions." ORDER BY cr.replydate DESC limit $page[limit]";
		$weiboreplylist=$this->_weiboreply_mod->db->getAll($sql);


		$sql="SELECT COUNT(1) FROM cw_weiboreply AS cr
                LEFT JOIN cw_member AS cm ON cr.uid=cm.user_id
                LEFT JOIN cw_weibo AS cw ON cw.wei_id = cr.wei_id
                WHERE 1=1 ".$conditions;
		$count=$this->_weiboreply_mod->db->getOne($sql);

		$page['item_count']=$count;   //获取统计数据

		$this->_format_page($page);
        $this->assign('search_options', $search_options);
        $this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
            'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
		$this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
		$this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条
		$this->assign('weiboreplylist', $weiboreplylist);
		$this->display('weiboreply.index.html');
	}
	function drop()
	{
		$article_ids = isset($_GET['id']) ? trim($_GET['id']) : '';
		if (!$article_ids)
		{
			$this->show_warning('no_such_weibo');

			return;
		}
		$article_ids=explode(',', $article_ids);
		$this->_weiboreply_mod->drop($article_ids);
		if ($this->_weiboreply_mod->has_error())    //删除
		{
			$this->show_warning($this->_weiboreply_mod->get_error());

			return;
		}

		$this->show_message('drop_ok');
	}
}